__author__ = 'tyrion'

from BaseStatus import BaseStatus
from src.common import GetHttpRequest
import  gloal_conf
import  json

class InstanceStatus(BaseStatus):

    def __init__(self, config):
        self.svrName = config.get('svrname', "error: not found svr name")
        self.instanceName = config.get("instancename", "error: not found instance name")
        return

    def GetStatus(self):
        #http://localhost:8000/check_mk_api/getoneservice/?hostname=10.33.6.130&servicename=CPU load
        # url = "http://%s/check_mk_api/getoneservice/?hostname=%s&servicename=%s" %(gloal_conf.MONITOR_IP, self.svrName, self.instanceName)
        url = "http://%s/check_mk/view.py?view.py?_do_confirm=yes&_username=icinga_nd&_secret=1234567890&_transid=-1&output_format=json&_do_actions=yes&view_name=service&host=%s&service=%s" %(gloal_conf.MONITOR_IP, self.svrName, self.instanceName)
        print "info: url = %s" %(url)
        ret = GetHttpRequest(url)
        print("info: http request ret=%s"%(ret))
        jsonRet = json.loads(ret)
        # print jsonRet[0]
        for index, item in enumerate(jsonRet[0]):
            if item == 'service_state':
                try:
                    stateRet =  jsonRet[1][index]
                    result = {self.instanceName: stateRet}
                    break
                except IndexError:
                    result = "error : check is the instance exit?"
            # result = {self.instanceName: stateRet}
        return result

    # def GetPerData(self):
    #     print("info: get instance status perf data")
    #     return
